package mipushdemo.xiaomi.com.diyitonghang.ui.fragment.home;

import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import mipushdemo.xiaomi.com.diyitonghang.R;
import mipushdemo.xiaomi.com.diyitonghang.base.BaseActivity;
import mipushdemo.xiaomi.com.diyitonghang.data.bean.InfoBean;
import mipushdemo.xiaomi.com.diyitonghang.ui.activity.MainActivity;
import mipushdemo.xiaomi.com.diyitonghang.utils.RetrofitUtils;

public class InfoActivity extends BaseActivity {
    @BindView(R.id.infoToolbar)
    Toolbar infoToolbar;
    @BindView(R.id.info_title)
    TextView infoTitle;
    @BindView(R.id.info_time)
    TextView infoTime;
    @BindView(R.id.info_image)
    ImageView infoImage;
    @BindView(R.id.tv_infoMsg)
    TextView tvInfoMsg;
    @BindView(R.id.tv_share)
    TextView tvShare;
    private String newsId;

    @Override
    protected int windowColor() {
        return R.color.colorWhite;
    }

    @Override
    protected void init() {
        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }
        changeStatusBarTextColor(true);
        Intent intent = getIntent();
        newsId = intent.getStringExtra("newsId");
        infoToolbar.setTitle("");
        setSupportActionBar(infoToolbar);
        infoToolbar.setNavigationIcon(R.drawable.close);
        infoToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        tvShare.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent textIntent = new Intent(Intent.ACTION_SEND);
                textIntent.setType("text/plain");
                textIntent.putExtra(Intent.EXTRA_TEXT, "http://www.baidu.com");
                startActivity(Intent.createChooser(textIntent, "分享一个链接"));
            }
        });
    }

    @Override
    protected void loadData() {
        RetrofitUtils.newInstance().netWorkRequest().infoData(MainActivity.userId, newsId).observeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<InfoBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(InfoBean infoBean) {
                        infoTitle.setText(infoBean.getData().getTitle());
                        infoTime.setText(infoBean.getData().getPublishTime());
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Glide.with(InfoActivity.this).load("http://cn.ttfly.com/file/upload/201805/17/1838257812249.jpg").into(infoImage);
                            }
                        });
                        tvInfoMsg.setText(infoBean.getData().getContent());
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e("--------",e.getMessage()+"==="+e.toString());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_info;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
